package com.ruoyi.web.controller.school;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.school.domain.PacReslutLevel;
import com.ruoyi.school.domain.vo.PacResultLevelImportVO;
import com.ruoyi.system.service.ReslutLevelService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/school/level")
public class ResultLevelController extends BaseController {
    @Resource
    private ReslutLevelService reslutLevelService;

    @GetMapping("/list")
    public TableDataInfo list(PacReslutLevel rereslutLevel) {
        startPage();
        List<PacReslutLevel> list = reslutLevelService.getList(rereslutLevel);
        return getDataTable(list);
    }

    @GetMapping("/details")
    public AjaxResult details(PacReslutLevel rereslutLevel) {
        PacReslutLevel details = reslutLevelService.getDetails(rereslutLevel);
        return success(details);
    }

    @Log(title = "学术等级", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody PacReslutLevel rereslutLevel) {
        boolean add = reslutLevelService.add(rereslutLevel);
        return success(add);
    }

    @Log(title = "学术等级", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult update(@RequestBody PacReslutLevel rereslutLevel) {
        boolean update = reslutLevelService.edit(rereslutLevel);
        return success(update);
    }

    @Log(title = "学术等级", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete")
    public AjaxResult delete(PacReslutLevel rereslutLevel) {
        boolean delete = reslutLevelService.delete(rereslutLevel);
        return success(delete);
    }

    @GetMapping("/all")
    public AjaxResult all(PacReslutLevel rereslutLevel) {
        List<PacReslutLevel> list = reslutLevelService.getAllList(rereslutLevel);
        return success(list);
    }

    /**
     * 导出模板
     */
    @PostMapping("/exportTemplate1")
    public void exportTemplate1(HttpServletResponse response) {
        ExcelUtil<PacResultLevelImportVO> util = new ExcelUtil<>(PacResultLevelImportVO.class);
        List<PacResultLevelImportVO> templateList = reslutLevelService.getAll();
        templateList.forEach(template -> {
            if (template.getHeightLevel() == 1) {
                template.setIsRk("是");
            } else {
                template.setIsRk("否");
            }
        });
        util.exportExcel(response, templateList, "学生数据");
    }

    /**
     * 导入数据
     */
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, Integer type) throws Exception {
        String message;

        ExcelUtil<PacResultLevelImportVO> util = new ExcelUtil<>(PacResultLevelImportVO.class);
        String operName = getUsername();
        List<PacResultLevelImportVO> studentList = util.importExcel(file.getInputStream());
        message = reslutLevelService.importData(studentList, operName);
        return success(message);
    }

    /**
     * 下载模板
     */
    @PostMapping("/exportTemplate")
    public void exportTemplate(HttpServletResponse response) {
        ExcelUtil<PacResultLevelImportVO> util = new ExcelUtil<>(PacResultLevelImportVO.class);
        List<PacResultLevelImportVO> templateList = new ArrayList<>();
        PacResultLevelImportVO studentInfo = new PacResultLevelImportVO();
        studentInfo.setResName("测试赛事");
        studentInfo.setResName2("测试子赛事");
        studentInfo.setResLevel("一级");
        studentInfo.setResType("软件");
        studentInfo.setIsRk("是");
        studentInfo.setHeightLevel(1);

        templateList.add(studentInfo);
        util.exportExcel(response, templateList, "竞赛数据");
    }

}
